package com.tangtang.core.crypt;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by yuanyue on 15/7/26.
 */
public class HMACUtil {
    /**
     * 初始化HMAC密钥
     *
     * @return
     * @throws Exception
     */
    public static String initMacKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA256");

        SecretKey secretKey = keyGenerator.generateKey();
        return Base64Util.encodeBase64String(secretKey.getEncoded());
    }

    /**
     * HMAC加密
     *
     * @param data
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encryptHMAC(byte[] data, String key, String method) throws Exception {

        //SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), "HmacMD5");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), method);//直接用给定的字符串进行
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);

        return mac.doFinal(data);

    }
}
